package com.web.kdl.controller.auth;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.kdl.common.result.CommonResult;
import com.web.kdl.entity.auth.PermissionInfo;
import com.web.kdl.entity.auth.RoleInfo;
import com.web.kdl.pojo.form.RoleForm;
import com.web.kdl.pojo.form.RoleQueryForm;
import com.web.kdl.pojo.form.RoleUpdateForm;
import com.web.kdl.service.auth.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author： shulibin
 * @Date： 2025/7/30 13:45
 * @Describe：
 */
@RestController
@RequestMapping("/roles")
@RequiredArgsConstructor
public class RoleController {
    @Resource
    RoleService roleService;


    /**
     * 获取角色列表
     * @return
     */
    @PostMapping("/getListRoles")
    public CommonResult<List<RoleInfo>> listRoles() {
        return CommonResult.success(roleService.listRoles());
    }

    /**
     * 获取角色详情
     * @return
     */
    @PostMapping("/getRole/{id}")
    public CommonResult<RoleInfo> getRole(@PathVariable String id) {
        RoleInfo role = roleService.getById(id);
        return CommonResult.success(role);
    }

    /**
     * 创建角色
     * @return
     */
    @PostMapping("/createRole")
    public CommonResult createRole(@RequestBody RoleForm form) {
        return roleService.save(form);
    }

    /**
     * 修改角色
     * @return
     */
    @PostMapping("/updateRole")
    public CommonResult updateRole(@RequestBody RoleUpdateForm form) {
        return roleService.updateById(form);
    }


    /**
     * 删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/deleteRole/{id}")
    public CommonResult deleteRole(@PathVariable String id) {
        return roleService.removeById(id);
    }
    /**
     * 获取角色拥有的权限列表
     * @param id 角色ID
     * @return 权限列表
     */
    // 获取角色权限
    @GetMapping("/getRolePermissions/{id}")
    public CommonResult<List<String>> getRolePermissions(@PathVariable String id) {
        return CommonResult.success(roleService.getRolePermissions(id));
    }
    /**
     * 为角色分配权限
     * @param roleId 角色ID
     * @param permissionIds 权限ID列表
     * @return 分配结果
     */
    @PostMapping("/assignPermissions/{id}")
    public CommonResult assignPermissions(@PathVariable String roleId,
                                          @RequestBody List<String> permissionIds) {
        return   roleService.assignPermissions(roleId, permissionIds);
    }


    /**
     * 添加分页查询角色接口
     * @param queryForm
     * @return
     */
    @PostMapping("/getPageRoles")
    public CommonResult<Page<RoleInfo>> getPageRoles(@RequestBody RoleQueryForm queryForm) {
        return CommonResult.success(roleService.pageRoles(queryForm));
    }
    /**
     * 添加批量删除角色接口
     * @param  ids
     * @return
     */
    @DeleteMapping("/batchDeleteRoles")
    public CommonResult batchDeleteRoles(@RequestBody List<String> ids) {
        return roleService.removeBatchByIds(ids);
    }

    /**
     * 移除角色的某个权限
     * @param roleId 角色ID
     * @param permissionId 权限ID
     * @return 移除结果
     */
    @DeleteMapping("/roles/{roleId}/permissions/{permissionId}")
    public CommonResult removePermissionFromRole(
            @PathVariable String roleId,
            @PathVariable String permissionId) {
        return roleService.removePermission(roleId, permissionId);
    }

}
